//Shirasu Azusa 2024.6
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x)  {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x)  {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x)  {return (x==0?-1:__builtin_ctzll(x));}

#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;  
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} 
  return x*w;
}

const int N=1e5+5,lim=1e5;
int n,x[N],y[N],w[N],m,cnt,d[N],ggg,sz[N],t[N],k;
vp g[N];
bool ok[N];
bitset<3005> pre[3005],suf[3005];

int dis(int i,int j) {
  return abs(x[i]-x[j])+abs(y[i]-y[j]);
}

namespace Brute {
  void solve() {
    int ans=0,ans2=0,ans3=0;
    rep(i,1,n) {
      rep(j,i+1,n) {
        int dd=dis(i,j);
        if(ok[dd]) {
          rep(k,1,n) if(k!=i&&k!=j) {
            int di=dis(i,k), dj=dis(k,j);
            if(di==dd||dj==dd) {
              if(di!=dj) ans2+=w[i]+w[j]+w[k];
              else ans3+=w[i]+w[j]+w[k];
            } else {
              if(di<dd&&dd<dj||di>dd&&dd>dj)
                ans+=w[i]+w[j]+w[k], ggg++;
            }
          }
        }
      }
    }
    printf("%lld\n",ans+ans2/2+ans3/3);
  }
}

int C3(int x) {
  return x*(x-1)*(x-2)/6;
}
int C2(int x) {
  return x*(x-1)/2;
}

namespace Sub2 {
  void solve() {
    rep(i,1,2*m) g[i].clear(), sz[i]=0;
    rep(i,1,n) rep(j,i+1,n) {
      int dd=dis(i,j);
      sz[dd]++;
    }
    rep(i,1,2*m) g[i].reserve(sz[i]);
    rep(i,1,n) rep(j,i+1,n) {
      g[dis(i,j)].eb(i,j);
    }
    rep(i,1,n) pre[i].reset();
    rep(i,1,n) rep(j,1,n) if(i!=j) suf[i].set(j);
    int ans=0; k=0;
    rep(i,1,2*m) if(ok[i]) t[++k]=i;
    rep(i,1,2*m) {
      for(pii pp:g[i-1]) {
        int x=pp.fi, y=pp.se;
        pre[x][y]=1, pre[y][x]=1;
      }
      for(pii pp:g[i]) {
        int x=pp.fi, y=pp.se;
        suf[x][y]=0, suf[y][x]=0;
      }
      if(ok[i]) {
        for(pii pp:g[i]) {
          int x=pp.fi, y=pp.se;
          auto kk=(pre[x]&suf[y])|(suf[x]&pre[y]);
          ans+=kk.count();
        }
      }
    }
    int ans2=0,ans3=0;
    rep(i,1,n) {
      rep(i,1,k) g[t[i]].clear(), sz[t[i]]=0;
      rep(j,1,n) if(i!=j) {
        int dd=dis(i,j);
        sz[dd]+=ok[dd];
      }
      rep(i,1,k) g[t[i]].reserve(sz[t[i]]);
      rep(j,1,n) if(i!=j) {
        int dd=dis(i,j);
        if(ok[dd]) g[dd].eb(i,j);
      }
      rep(j,1,k) {
        int i=t[j];
        for(pii p1:g[i]) for(pii p2:g[i]) if(p1<p2) {
          int x=p1.se, y=p2.se;
          if(dis(x,y)!=i) ans2++;
          else ans3++; 
        }
      }
    }
    printf("%lld\n",ans*3+ans2*3+ans3);
  }
}

void work() {
  n=read(), m=read();
  rep(i,1,n) x[i]=read(), y[i]=read();
  rep(i,1,n) w[i]=read();
  if(n<=100) {
    Brute::solve();
    return;
  }
  Sub2::solve();
}

signed main() {
  freopen("chess.in","r",stdin);
  freopen("chess.out","w",stdout);
  rep(i,2,lim) ok[i]=1;
  rep(i,2,lim) {
    for(int j=2*i;j<=lim;j+=i) ok[j]=0;
  }
  rep(i,11,lim) ok[i]&=(i%20==3);
  int T=read();
  while(T--) work();
  return 0;
}